﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Core.Distribution;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.AppVersion;
using FwProject.Core.Car;
using FwProject.Car;
using FwProject.Core.FactoryInfo;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.TypeNumber;
using Castle.Core.Logging;
using Abp.ObjectMapping;
using Abp.ElasticSearch;
using FwProject.BrandInfo;
using System.Threading.Tasks;
using FwProject.BrandInfo.Dto;
using FwProject.ShoppingMall.Shop;
using FwProject.Shop.Dto;
using FwProject.Shop;
using FwProject.ShoppingMall.EventAdvertising;
using FwProject.Comm;

namespace FwProject.ShopApplication.RemoteEventBus
{
    /// <summary>
    ///餐厅缓存数据
    /// </summary>
    [RemoteEventHandler(ForType = RemoteTypeHelp.TYPESHOPORDERCOUPON, ForTopic = RemoteTypeHelp.TOPICAPPSYSTEMSHOPORDERCOUPON)]
    public class RemoteEventBusShopOrderCoupon : IRemoteEventHandler, ITransientDependency
    {
        public ICacheManager _catchmanager { get; set; }
        private readonly IRepository<ShopOrderCoupon, Guid> _repositoryShopOrderCoupon;
        private readonly IRepository<ShopUserCoupon, Guid> _repositoryShopUserCoupon;
        public IElasticsearch _elasticsearch;
    
        public ILogger iLogger { get; set; }

        public IObjectMapper _objectMapper { get; set; }

        public RemoteEventBusShopOrderCoupon(
              IRepository<ShopOrderCoupon, Guid> repositoryShopOrderCoupon,
              IElasticsearch elasticsearch,
              IRepository<ShopUserCoupon, Guid> repositoryShopUserCoupon

            )
        {
            _repositoryShopUserCoupon = repositoryShopUserCoupon;
             _elasticsearch = elasticsearch;
            _repositoryShopOrderCoupon = repositoryShopOrderCoupon;
            _objectMapper = NullObjectMapper.Instance;
          
        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            try
            {
                iLogger.Info($"优惠券==={eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString()}===已使用优惠券");
                var model= _repositoryShopOrderCoupon.Get(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()));
                var m=_repositoryShopUserCoupon.Get(model.ShopUserCouponId);
                m.UseStatus = UseStatus.Used;

            }
            catch (Exception exe)
            {
                iLogger.Info($"优惠券==={eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString()}===优惠券", exe);
            }
        }
    }
}
